package com.zjdiepu.www.biz.icbc;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.icbc.YwOpenCardHzicbcSubmitInfoMapper;
import com.zjdiepu.www.ibiz.icbc.IYwOpenCardHzicbcSubmitInfoService;
import com.zjdiepu.www.model.icbc.dto.YwOpenCardHzicbcSubmitInfo;
import com.zjdiepu.www.model.icbc.ext.YwOpenCardHzicbcSubmitInfoExt;

/**
 * yw_开卡信息_杭州工行_提交信息Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2018年03月16日 14时11分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwOpenCardHzicbcSubmitInfoServiceImpl extends BaseService implements IYwOpenCardHzicbcSubmitInfoService {
	public static final Logger logger = LoggerFactory.getLogger(YwOpenCardHzicbcSubmitInfoServiceImpl.class);

	@Autowired
	private YwOpenCardHzicbcSubmitInfoMapper ywOpenCardHzicbcSubmitInfoMapper;
	
	@Override
	public void saveUpdateAndDelete(YwOpenCardHzicbcSubmitInfo ywOpenCardHzicbcSubmitInfo, OperateType operType) {
		switch (operType) {
		case add:
			ywOpenCardHzicbcSubmitInfo.initInsertData(ywOpenCardHzicbcSubmitInfo.getModifier(), ywOpenCardHzicbcSubmitInfo.getModifierName());
			ywOpenCardHzicbcSubmitInfoMapper.insert(ywOpenCardHzicbcSubmitInfo);
			break;
		case edit:
			ywOpenCardHzicbcSubmitInfo.initUpdateData(ywOpenCardHzicbcSubmitInfo.getModifier(), ywOpenCardHzicbcSubmitInfo.getModifierName());
			// 影响的记录数
			int updateCount = ywOpenCardHzicbcSubmitInfoMapper.updateByPrimaryKeySelective(ywOpenCardHzicbcSubmitInfo);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywOpenCardHzicbcSubmitInfo.initDeleteData(ywOpenCardHzicbcSubmitInfo.getModifier(), ywOpenCardHzicbcSubmitInfo.getModifierName());
			// 影响的记录数
			int deleteCount = ywOpenCardHzicbcSubmitInfoMapper.updateByPrimaryKeySelective(ywOpenCardHzicbcSubmitInfo);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwOpenCardHzicbcSubmitInfo> selectByExt(YwOpenCardHzicbcSubmitInfoExt ywOpenCardHzicbcSubmitInfoExt) {
		return ywOpenCardHzicbcSubmitInfoMapper.selectByExt(ywOpenCardHzicbcSubmitInfoExt);
	}
	
	@Override
	public int countByExt(YwOpenCardHzicbcSubmitInfoExt ywOpenCardHzicbcSubmitInfoExt){
		return ywOpenCardHzicbcSubmitInfoMapper.countByExt(ywOpenCardHzicbcSubmitInfoExt);
	}
	
	@Override
	public YwOpenCardHzicbcSubmitInfo selectById(String id) {
		return ywOpenCardHzicbcSubmitInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywOpenCardHzicbcSubmitInfoMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwOpenCardHzicbcSubmitInfo ywOpenCardHzicbcSubmitInfo) {
		return ywOpenCardHzicbcSubmitInfoMapper.countRefined(ywOpenCardHzicbcSubmitInfo);
	}

	@Override
	public YwOpenCardHzicbcSubmitInfo selectRefined(YwOpenCardHzicbcSubmitInfo ywOpenCardHzicbcSubmitInfo) {
		return ywOpenCardHzicbcSubmitInfoMapper.selectRefined(ywOpenCardHzicbcSubmitInfo);
	}
	
	@Override
	public int deletedRefined(YwOpenCardHzicbcSubmitInfo ywOpenCardHzicbcSubmitInfo) {
		return ywOpenCardHzicbcSubmitInfoMapper.deletedRefined(ywOpenCardHzicbcSubmitInfo);
	}

	@Override
	public boolean repeat(YwOpenCardHzicbcSubmitInfo ywOpenCardHzicbcSubmitInfo) {
		int count = countRefined(ywOpenCardHzicbcSubmitInfo);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywOpenCardHzicbcSubmitInfo.getId())) {
			return true;
		}
		YwOpenCardHzicbcSubmitInfo dbItem = selectRefined(ywOpenCardHzicbcSubmitInfo);
		if(null == dbItem) {
			return false;
		}
		return !ywOpenCardHzicbcSubmitInfo.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywOpenCardHzicbcSubmitInfoMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwOpenCardHzicbcSubmitInfo> ywOpenCardHzicbcSubmitInfos) {
		return ywOpenCardHzicbcSubmitInfoMapper.insertBatch(ywOpenCardHzicbcSubmitInfos);
	}
}
